package com.wswy.wzcx.utils.loader;

import android.view.View;
import java.util.HashMap;

/**
 * Tag兼容，可添加多个tag
 *
 * @author lizhangqu
 * @version V1.0
 * @since 2017-07-30 13:08
 */
class TagCompat {

    private static final String KEY_DRAWEE_HOLDER = "DraweeHolder";

    public static void setTag(View view, Object tag) {
        setTag(view, KEY_DRAWEE_HOLDER, tag);
    }

    public static Object getTag(View view) {
        return getTag(view, KEY_DRAWEE_HOLDER);
    }

    public static boolean setTag(View view, String tagKey, Object tagObj) {
        if (view != null) {
            Object tag = view.getTag();
            if (tag instanceof TagMap) {
                ((TagMap) tag).put(tagKey, tagObj);
                return true;
            }
            if (tag == null) {
                view.setTag(new TagMap(tagKey, tagObj));
                return true;
            }
        }
        return false;
    }

    public static Object getTag(View view, String tagKey) {
        if (view != null) {
            Object tag = view.getTag();
            if (tag instanceof TagMap) {
                Object value = ((TagMap) tag).get(tagKey);
                if (value != null) {
                    return value;
                }
            }
        }
        return null;
    }



    private static class TagMap extends HashMap<String, Object> {
        private TagMap(String key, Object value) {
            put(key, value);
        }
    }
}